"use strict";
/* Controllers */
app.controller("hdfsPathListController", ['$scope', '$compile', '$location', '$rootScope', '$routeParams', 'hdfsPathListService', function ($scope, $compile, $location, $rootScope, $routeParams, hdfsPathListService) {

    var vm = $scope.vm;
    vm.p = vm.result = {};

    vm.p.type = $routeParams.type;
    vm.p.project = $routeParams.project;

    if(vm.p.type=='5') {
        vm.p.depth = 5;
    }else {
        vm.p.depth = 4;
    }
    if(vm.p.project=='0') {
        vm.p.project = '';
    }

    var errorFn = function (result) {
        alert(result);
    };

    $scope.listHdfsPath = function () {
        var vm = $scope.vm;
        var params = {
            depth: vm.p.depth,
            project: vm.p.project
        };
        var procFn = function (response) {
            var data = response.data;
            if (data == null) {
                return;
            }
            vm.result.data = data;
            $('#list_table').bootstrapTable('load', data);
        };
        hdfsPathListService.listHdfsPath(params, procFn, errorFn);
    };

    $scope.initializeController = function () {
        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
            pageList: [30, 50, 100, 300, 500, 1000], pageSize: 30
        });
        if(vm.p.depth==4) {
            $resultTable.bootstrapTable('hideColumn', 'app');
        }
        $scope.listHdfsPath();
    };

    $scope.initializeController();

}]);

/* Services */
app.service('hdfsPathListService', ['ajaxService', function (ajaxService) {

    this.listHdfsPath = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/hdfsPath/list.do", successFunction, errorFunction);
    };

}]);

function hdfsPathOpt(value, row, index) {
    return [
        '<a type="button" class="btn btn-xs btn-primary" href="#/sre/mng/hdfs_path_edit/',row.depth,'/',row.id,'" target="_blank">修改</a> '
    ].join('');
}

function projectFmt(value, row, index) {
    return [
        '<a href="#/sre/mng/hdfs_path_list/5/',value,'" target="_blank">',value,'</a> '
    ].join('');
}

function hdfsPathFmt(value, row, index) {
    var fullPath =  [
        row.hdfsBasicPath,
        row.hdfsDataPath,
        row.project
    ].join('');

    if(row.depth==4) {
        return fullPath;
    }

    return [
        fullPath, '/',row.app
    ].join('');
}

